home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / js.zip / APL91.JS < prev    next >
Text File  |  1992-12-30  |  7KB  |  303 lines

  1. NB.  This file can be used as a script input file to J Version 6.1
  2. NB.  December 1992
  3.  
  4. NB.  Donald B. McIntyre
  5. NB.  Luachmhor, 1 Church Road
  6. NB.  KINFAUNS, PERTH PH2 7LD
  7. NB.  SCOTLAND - U.K.
  8. NB.  Telephone:  In the UK:      0738-86-726
  9. NB.  From USA and Canada:   011-1-738-86-726
  10. NB.  email:  donald.mcintyre@almac.co.uk
  11.  
  12. NB.   "Mastering J"  APL91, APL Quote Quad 21#4 (Aug 91) p.264-273
  13.  
  14. NB.   p.265
  15.    power=. ^
  16.    rotate=. |.
  17.    NB.   mean=. m.   Removed after Version 3.1
  18.    gradeup=. /:
  19.    ip=. +/ .*        NB. space required between / and .
  20.    
  21. NB.  A period (.) preceded by a space is a conjunction
  22. NB.   1 2 3 4 .5 6 7     Gives Domain Error
  23. NB.   3 + .5             Gives Domain Error
  24.    x=. .5
  25.    x
  26.    i.5
  27.    i=. i. 5
  28.    
  29.    x =. .*
  30.    sum=. +/
  31.    ip=. sum x
  32. NB.   A verb was formerly "fixed" in terms of J primitives.
  33. NB.   This is no longer the default;  adverb f. now fixes a verb
  34.    ip
  35.    ip=. ip f.
  36.    ip
  37.    1 2 3 ip 3 4 5
  38.    
  39.    |:5       NB.   This no longer gives a Length Error
  40. NB.  (,5) + i. 4     Still gives a Length Error
  41.    
  42.    setrl=. 9!:1    NB. Set Random Link.   See p.271
  43.    setrl 7^5
  44.    qrl=. 9!:0      NB. Query Random Link.  See p.272
  45.    qrl 0
  46.    ]y=. ?10$100
  47. NB. p.266
  48.    /:y
  49.    y/:y
  50.    sort=. /:~
  51.    sort y
  52.  
  53.    setrl 7^5
  54.    qrl 0
  55.    y=. 'abcde'{~?12$4
  56.    qrl 0
  57. NB.  The Random Link was set differently for APL QUOTE QUAD
  58.    ]y=. 'abcde'{~?12$4
  59.    sort y
  60.    ~.y
  61.    ~:y
  62.    
  63.    ]a=. i. 2 3 4
  64.    $a
  65.    #$a
  66.    {.$a
  67.    #a
  68.    ]z=. |. a
  69.    z-: |."3 a
  70.    
  71.    |."2 a
  72.    |."1 a
  73. NB. p.267
  74.    x=. i. 5
  75.    y=. i. 5 3
  76.    ]z=. x, "0 1 y
  77.    z-: x,"_1 y
  78.    
  79.    x=. i. 5 2
  80.    ]z=. x,"1 1 y
  81.    z-: x,"1 y
  82.    z-: x,"_1 y
  83.    
  84.    f0=. ,&0"1
  85.    a=. i. 3 2 4
  86.    f0 a
  87.    f0
  88.  
  89.    
  90.    mean=. +/%#
  91.    mean
  92. NB.   (+/%#) y   is a fork:    (+/ y) % (# y)
  93.  
  94.    setrl 7^5
  95.    ]y=. ?14$100
  96.  
  97.    y- (mean y)
  98.  
  99. NB.  p.268   Note the major change in default for "evoke"
  100.    deviations=. - 'mean'~
  101.    deviations
  102.    
  103. NB.   The default has been changed.  Adverb f. now fixes a verb.
  104.    deviations=. - mean
  105.    deviations
  106.    deviations=. deviations f.
  107.    deviations
  108.  
  109.    t=. i.5 3
  110.    mean t           NB. mean of items
  111.  
  112. NB. With respect to rank-0 cells, the frames of t and (mean t)
  113. NB. are 5 3 and 3.   Early Versions of J used SUFFIX "agreement";
  114. NB. Version 5 uses PREFIX "agreement" (Dictionary p.6).
  115. NB. In this example the ranks must therefore be specified:
  116. NB. See also DEV.JS and RANK.JS
  117.    t- mean t             NB. ranks of - are 0 0
  118.  
  119.    ]z=. t-"1 1 mean t
  120.    dev=. -"1 mean        NB. For rank-2 argument
  121.    z-: dev t
  122.    dev i.2 3 4           NB. Not for rank other than 2
  123.    dev=. -"_1 _ mean     NB. For argument of any rank
  124.    z-: dev t
  125.    dev i.2 3 4           NB. Deviations of ITEMS from ITEM means
  126.    
  127. NB.     (f g h i j k)  is   f (g h (i j k))
  128.    4!:55 ;:'f g h i j k'
  129.    (f g h i j k)
  130.  
  131.    squares=. *:
  132.    sum=. +/
  133.    ]z=. sum squares deviations y
  134.    ss=. sum @ squares @ deviations
  135.    z-: ss y
  136.    ss=. ss f.
  137.    ss
  138.    z-: ss y
  139.    ss=. 'sum'~ @ ('squares'~) @ ('deviations'~)
  140.    z-: ss y
  141.    ss2=. 'sum'~ @ ('squares'~ @ ('deviations'~))
  142.    z-: ss2 y
  143.    ss2
  144.    
  145. NB. p.269
  146.    ss=. '+/ *: (- (+/ % #)) y.' : ''    NB. Parentheses required
  147.    z-: ss y
  148.    ss=. +/ @ *: @ (- +/%#)
  149.    z-:ss y
  150.    
  151.    ss=. '+/ *: - (+/ % #) y.' : '' NB. Two sets of parentheses required
  152.    ss y
  153.    ss=. '+/ *: - +/ % # y.' : ''
  154.    ss y
  155.    
  156. NB.  Inherited rank has been changed
  157. NB.  Scalar functions now have infinite rank
  158.    setrl 7^5
  159.    x=. ?12 5$100
  160.    colgrade=. /: @ ({"1"_)
  161.    3 colgrade x
  162.    (3 colgrade x){x
  163.    colgrade=. /: @ ({"1)
  164.    3 colgrade x
  165.    
  166. NB.  Scalar verbs are now given infinite rank
  167.    ss=. +/ @ squares
  168.    squares=. ^&2
  169.    ]z=. ss i.10
  170.    squares=. ^&2"0 
  171.    z-: ss i.10
  172.    squares=. ^&2"_
  173.    z-: ss i.10
  174.  
  175. NB.  power (^:)    cut (;.)   under (&.)  remain the same
  176. NB.  fit (&:)  is now   !.
  177.  
  178. NB.  p.270
  179.    v=. i. 15
  180.    u=. 15$1 0
  181.    u # v
  182.    
  183.    m=. i. 5 5
  184.    u=. 5$ 1 0
  185.    u#m
  186.    u#"1 m
  187.    
  188.    x=.6 7 8
  189. NB.  u.=1 0 1 0 0 1 0   should be:
  190.    u=.1 0 1 0 0 1 0
  191.    
  192.    u*+/\u
  193.    (u*+/\u){0,x
  194.    
  195.    expand=./:@\:@[{#@[{.]
  196.    expand
  197.    v=.1 0 1 0 1
  198.    b=.6 7 8
  199.    v expand b
  200.    
  201.    b=.i.3 4
  202.    v expand b
  203.    b=.i.3 4 5
  204.    v expand b
  205.    b=.3 4$'abcdef'
  206.    v expand b
  207.    b=.3 4 5$'abcdef'
  208.    v expand b
  209.    
  210.    a=.'abcdefg'
  211.    ]b=.u#a
  212.    ]c=.(-.u)#a
  213.    (/:\:u){b,c
  214.    
  215.    a=. 'sek'
  216.    b=. 'ta'
  217.    u=. 0 1 0 1 0
  218.    (/:\:u){b,a
  219.    
  220.    m=. i. 6 6
  221.    (<3 4){m
  222.    
  223. NB.   x i} m      Amend has been amended !
  224. NB.  Illustrations from Jacobi's method, which requires substitutions
  225. NB.  based on the "pivot" -- the largest absolute value off the diagonal.
  226.    
  227.    m=. i.6 6
  228.    ]i=. 2 2; 2 4; 4 2; 4 4
  229.    x=. 100 101 102 103
  230.    f=. >@[ +/ .* ,&1@#@]
  231.    i f m
  232.    x (i f m)}m
  233.    
  234.    ut=. ,@(</~@i.)@#            NB.  Upper Triangle
  235.    pt=. (, i. >./@(ut # ,))@|   NB.  Pivot in Triangle
  236.    pm=. <.@(pt % #) , # | pt    NB.  Pivot in Matrix
  237.    pa=. 0 0&{ ; ] ; |. ; 1 1&{  NB.  Permutations for amend
  238.    ia=. pa@pm { i.@$            NB.  Indices for Amend
  239.    amend=. ia@]}                NB.  Amend the right argument
  240.    
  241.    setrl 7^5
  242.    ]y=. 50-~ ?6 6$100
  243.    x amend y
  244.    
  245.    p=.  0 0&{ ; ] ; |. ; 1 1&{   NB.  permutations for amend
  246.    ia=. p@pm { i.@$              NB.  indices for amend
  247.    id=. =/~ i.6
  248.    x (ia y)} id
  249.    
  250. NB.  p.271
  251. NB.         b /: > a {"1 b
  252. NB.  '' : 'y. /: > x. {"1 y.'
  253.    
  254.    
  255. NB.  y. /: > x. {"1 y
  256. NB.  (x f y) g (x h y)
  257.    
  258. NB.   > x. }"1 y      ] /: >@({"1) .
  259.    
  260.    SOCe=. '' : 'y. /: > x. {"1 y.'
  261.    SOCt=. ] /: >@({"1)
  262.    
  263.    read=. (1!:1)&<
  264. NB.   x=. read 'c:\s\j3\util.in'
  265.  
  266. NB.  p.272
  267. NB.  Ravel Items, Raze, and Definition are changed, hence:
  268.    
  269.    x +/ x=. 3 4 5 6
  270.    over=. ({. ; }.)@":@,
  271.    a=. x over x+/x
  272.  
  273.    h=. ' '&; @ ,.            NB.  Monadic
  274.    g=. ,~"_1                 NB.  Dyadic
  275.    ]z=. a g (h x)
  276.    z-: a (g h) x                 NB. Hook
  277.    by=. (g h)~
  278.    z-: x by a
  279.    by=. (,~"_1 ' '&;@,.)~
  280.    z-: x by x over x+/x
  281.    table=.  '] by ] over ] x.f./ ]':1
  282.    z-: + table x
  283.  
  284.    NB.  Alternative forms:
  285.    over0=. ({. ; }.)@":@,
  286.    over1=. ,.@({.;}.)@":@,
  287.    by0=. (,~"_1 ' '&;@,.)~
  288.    by1=. ' '&;@,.@[ ,. ]
  289.  
  290.    table=.  '] by0 ] over0 ] x.f./ ]':1
  291.    z=. + table x
  292.    table=.  '] by0 ] over1 ] x.f./ ]':1
  293.    z-: + table x
  294.    table=.  '] by1 ] over0 ] x.f./ ]':1
  295.    z-: + table x
  296.    table=.  '] by1 ] over1 ] x.f./ ]':1
  297.    z-: + table x
  298.  
  299.    over0       NB. Contains a fork
  300.    over1       NB. Ravel items changed from ; to ,. (But not needed)
  301.    by0         NB. Hook
  302.    by1         NB. Fork
  303.